package com.tronsis.distribution.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.me.order.MyStockActivity;
import com.tronsis.distribution.bean.NoticeBean;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.util.StateUtil;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.MultiItemTypeAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.adapter.recyclerview.wrapper.LoadMoreWrapper;
import com.tronsis.library.http.ListBean;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DateUtil;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.SRFConfigUtil;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_message)
    RecyclerView mRv;
    @BindView(R.id.srl_message)
    SwipeRefreshLayout mSrl;

    private List<NoticeBean> mNoticelList = new ArrayList<>();
    private int mPage = 1;
    private int mSize = 15;
    private int mTotal;
    private LoadMoreWrapper mLoadMoreWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.my_message));
        setRightTvText(getString(R.string.setting));

        initRv();
        getNotice(mPage, mSize, true);
    }

    private void initRv() {
        SRFConfigUtil.initSwipeRefreshLayout(mSrl);
        mSrl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getNotice(mPage = 1, mSize = 15, true);
            }
        });

        CommonAdapter<NoticeBean> adapter = new CommonAdapter<NoticeBean>(mContext, mNoticelList, R.layout.item_message) {
            @Override
            protected void convert(ViewHolder holder, NoticeBean noticeBean, int position) {
                holder.setText(R.id.tv_createdOn, DateUtil.getDateTimeString(noticeBean.getCreatedOn()));
                TextView tvContent = holder.getView(R.id.tv_content);
                if (!TextUtils.isEmpty(noticeBean.getType()) && StringUtil.isEquals(noticeBean.getType(), StateUtil.MSG_ASK_PAY) || StringUtil.isEquals(noticeBean.getType(), StateUtil.MSG_ASK_PURCHASE) || StringUtil.isEquals(noticeBean.getType(), StateUtil.MSG_VERIFY_PURCHASE)) {
                    String content = String.format(noticeBean.getContent() + "<font color=\"#ED5454\">%s", "查看详情。");
                    tvContent.setText(Html.fromHtml(content));
                } else {
                    tvContent.setText(noticeBean.getContent());
                }
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                NoticeBean noticeBean = mNoticelList.get(position);
                String type = noticeBean.getType();
                String typeId = noticeBean.getTypeId();
                Intent intent = new Intent();
                if (!TextUtils.isEmpty(type)) {
                    // TODO: 2019/5/17 跳转详情
                    switch (type) {
                        case StateUtil.MSG_SYSTEM:
                            break;
                        case StateUtil.MSG_TEAM:
                            break;
                        case StateUtil.MSG_ASK_PAY:
                            intent.setClass(mContext, AskPaySubmitActivity.class);
                            intent.putExtra("askPayId", typeId);
                            startActivity(intent);
                            break;
                        case StateUtil.MSG_BACKEND:
                            break;
                        case StateUtil.MSG_ASK_PURCHASE:
                            intent.setClass(mContext, MyStockActivity.class);
                            if (noticeBean.getContent() != null) {
                                if (noticeBean.getContent().contains("未通过")) {
                                    intent.putExtra("index", 3);
                                }else {
                                    intent.putExtra("index", 2);
                                }
                            }else {
                                intent.putExtra("index", 0);
                            }
                            startActivity(intent);
                            break;
                        // TODO: 2019/5/17 未通过
                        case "ASK_PURCHASE_REJECT":
                            intent.setClass(mContext, MyStockActivity.class);
                            intent.putExtra("index", 3);
                            startActivity(intent);
                            break;
                        case StateUtil.MSG_VERIFY_PURCHASE:
                            intent.setClass(mContext, StockProcessingActivity.class);
                            intent.putExtra("index", 1);
                            startActivity(intent);
                            break;
                        default:
                            break;
                    }
                }


            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mLoadMoreWrapper = new LoadMoreWrapper(adapter);
        mLoadMoreWrapper.setLoadMoreView(R.layout.rv_load_more);
        mLoadMoreWrapper.setOnLoadMoreListener(new LoadMoreWrapper.OnLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (mNoticelList.size() % 15 == 0 && mNoticelList.size() > 0 && mNoticelList.size() < mTotal) {
                    mPage = (mNoticelList.size() / 15) + 1;
                    getNotice(mPage, mSize, false);
                }
            }
        });

        int height = DisplayUtil.getInstance(mContext).dp2px(14);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRv.addItemDecoration(decoration);
        mRv.setLayoutManager(new LinearLayoutManager(mContext));
        mRv.setAdapter(mLoadMoreWrapper);
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        if (paramId == RIGHT_TEXT) {
            startActivity(new Intent(mContext, MessageSettingActivity.class));
        }
    }

    private void getNotice(int page, int size, final boolean isRefresh) {
        try {

            UserBizFactory.createBiz().getNotice(this, page, size, new UICallBack() {
                @Override
                public void onStart() {
                    mSrl.setRefreshing(true);
                }

                @Override
                public void onRefresh(Object data) {
                    mSrl.setRefreshing(false);

                    ListBean listBean = (ListBean) data;
                    mTotal = listBean.getTotal();
                    List<NoticeBean> list = listBean.getList();
                    if (isRefresh) {
                        mNoticelList.clear();
                        mNoticelList.addAll(list);
                    } else {
                        mNoticelList.addAll(list);
                    }
                    mLoadMoreWrapper.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    mSrl.setRefreshing(false);

                }
            });
        } catch (Exception e) {
            mSrl.setRefreshing(false);
            e.printStackTrace();
        }
    }
}
